package com.bkgy.controller.shutdowndetails.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.shutdowndetails.entity.TbDowntimeTcm2280;
import com.bkgy.controller.shutdowndetails.entity.dto.TcmDownTimeRes;
import com.bkgy.controller.shutdowndetails.mapper.ITbDowntimeTcm2280Mapper;
import com.bkgy.controller.shutdowndetails.service.ITbDowntimeTcm2280Service;
import com.bkgy.controller.tpdol3data.entity.OutputSec;
import com.bkgy.modular.tpdidata_cal1630.service.ITPdiDataMesService;
import com.bkgy.sys.common.constant.CommonConstant;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TbDowntimeTcm2280ServiceImpl extends ServiceImpl<ITbDowntimeTcm2280Mapper, TbDowntimeTcm2280> implements ITbDowntimeTcm2280Service {
    private final ITPdiDataMesService pdiMesService;

    @Override
    public ResultData tcmStop(String beginDate) {
        Date nextDay = DateUtil.getNextDayZero(DateUtil.getStringToDate("yyyy-MM-dd", beginDate));
        String endDate = DateUtil.getDateToString(nextDay, "yyyy-MM-dd");
        LambdaQueryWrapper<TbDowntimeTcm2280> w = new LambdaQueryWrapper<>();
        w.ge(TbDowntimeTcm2280::getStopEndTime, beginDate).lt(TbDowntimeTcm2280::getStopEndTime, endDate);
        List<TbDowntimeTcm2280> list = list(w);

        List<TcmDownTimeRes> resList = new ArrayList<>();

        for (TbDowntimeTcm2280 t : list) {
            TcmDownTimeRes res = new TcmDownTimeRes();
            res.setCrew(CommonConstant.crewMap.get(t.getCrew()));
            Date stopStartTime = t.getStopStartTime();
            Date stopEndTime = t.getStopEndTime();
            Integer duration = t.getDuration();
            res.setStopRange(DateUtil.getDateToString(stopStartTime, "HH:mm") + "-" + DateUtil.getDateToString(stopEndTime, "HH:mm"));
            res.setDuration(t.getDuration());
            res.setReason(t.getReason());
            OutputSec outputSec = pdiMesService.sumOutputSecInfo(stopStartTime, duration);
            res.setOutput(outputSec.getTotalOutput());

            resList.add(res);
        }
        return ResultData.ResultDataSuccess(resList, resList.size());
    }
}
